<template>
  <el-cascader
    size="mini"
    :options="treeData"
    :props="props"
    :value="value"
    separator="-"
    @change="changeValue"
  />
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { translateListToTreeData } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      props: {
        value: 'id', // 控制存储/请求接口(id: 接口文档中要求传递的部门id)
        label: 'name' // 控制显示
      }
    }
  },
  async created() {
    const res = await getDepartmentList()
    this.treeData = translateListToTreeData(res, 0)
  },
  methods: {
    // 选中节点改变
    changeValue(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style>

</style>
